package com.example.seckilldemo.rabbitmq;

import com.example.seckilldemo.pojo.SeckillMessage;
import com.example.seckilldemo.pojo.SeckillOrder;
import com.example.seckilldemo.pojo.User;
import com.example.seckilldemo.service.IGoodsService;
import com.example.seckilldemo.service.IOrderService;
import com.example.seckilldemo.utils.JsonUtil;
import com.example.seckilldemo.vo.GoodsVo;
import com.example.seckilldemo.vo.RespBean;
import com.example.seckilldemo.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQReceiver {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;

    @RabbitListener(queues = "seckillQueue")
    public void receive(String message){
        log.info("接收的消息：" + message);
        SeckillMessage seckillMessage = JsonUtil.jsonStr2Object(message, SeckillMessage.class);
        Long goodsId = seckillMessage.getGoodId();
        User user = seckillMessage.getUser();
        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodsId);
        if (goodsVo.getStockCount() < 1){
            return;
        }
        //判断是否重复抢购
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);
        if (seckillOrder != null) {
            return;
        }
        //下单操作
        orderService.seckill(user, goodsVo);
    }



    /*
    @RabbitListener(queues = "queue")
    public void receive(Object msg){
        log.info("接收消息: " + msg);
    }

    @RabbitListener(queues = "queue_fanout01")
    public void receiveF1(Object msg){
        log.info("QUEUE01接收消息: " + msg);
    }
    @RabbitListener(queues = "queue_fanout02")
    public void receiveF2(Object msg){
        log.info("QUEUE02接收消息: " + msg);
    }

    @RabbitListener(queues = "queue_direct01")
    public void receiveD1(Object msg){
        log.info("QUEUE01接收消息: " + msg);
    }
    @RabbitListener(queues = "queue_direct02")
    public void receiveD2(Object msg){
        log.info("QUEUE02接收消息: " + msg);
    }

    @RabbitListener(queues = "queue_topic01")
    public void receiveT1(Object msg){
        log.info("QUEUE01接收消息: " + msg);
    }
    @RabbitListener(queues = "queue_topic02")
    public void receiveT2(Object msg){
        log.info("QUEUE02接收消息: " + msg);
    }

    @RabbitListener(queues = "queue_headers01")
    public void receiveH01(Message msg){
        log.info("QUEUE01接受对象：" + msg);
        log.info("QUEUE01接受消息：" + new String(msg.getBody()));
    }
    @RabbitListener(queues = "queue_headers02")
    public void receiveH02(Message msg){
        log.info("QUEUE02接受对象：" + msg);
        log.info("QUEUE02接受消息：" + new String(msg.getBody()));
    }
     */

}
